﻿#include "DAConfigDialog.h"
#include "DASetting.h"
#include "PluginManager.h"
#include "qabstractbutton.h"
#include "qbuttongroup.h"
#include "qlabel.h"
#include "qlistwidget.h"
#include "qnamespace.h"
#include "qpushbutton.h"
#include "qstackedwidget.h"
#include "qwidget.h"
#include <QDebug>

#include <DAFuncTools.h>
#include <QCoreApplication>
#include <QGridLayout>
#include <QProcess>

DAConfigDialog::DAConfigDialog(QWidget *parent)
    :QDialog(parent)
{
    dasettings = new DASetting();
    setWindowFlags(Qt::WindowStaysOnTopHint | Qt::Dialog);
    setWindowTitle("桌面助手设置");
    setWindowIcon(QIcon(":/Style/image/da4.svg"));
    setWindowIconText("桌面助手设置");
    resize(dasettings->GetSizeValue(GENERAL_SIZE));
    
    InitLayout();
    BindBtnSlot();
}

void DAConfigDialog::InitLayout()
{
    MenuListBtn = new QButtonGroup(this);
    
    BtnGroup = new QButtonGroup(this);
    Confirm  = new QPushButton("确认",this);
    Cancel   = new QPushButton("取消",this);
    Apply    = new QPushButton("应用",this);

    BtnGroup->addButton(Confirm,0);
    BtnGroup->addButton(Cancel,1);
    BtnGroup->addButton(Apply,2);
    
    MenuList = new QListWidget(this);
    MenuList->setFixedWidth(135);
    SetPage = new QStackedWidget(this);
    
    LabTitle = new QLabel(this);
    LabTitle->setStyleSheet("font: bold;font-size:14px;color:bl");
    QTabWidget *BaseTab = new QTabWidget();
    
    BaseSet = new BaseConfig(this);
    PluginSet =new PluginManager(this);
    
    BaseTab->addTab(BaseSet,BaseSet->PluginName());
    BaseTab->addTab(PluginSet,PluginSet->PluginName());
    
    MenuListAddItem("基本设置",BaseTab);
    
    for (int var = 0; var < 15; ++var) {
        QString txt = QString("循环按钮").append(QString::number(var));
        MenuListAddItem(txt,var);    
    }
    
    QGridLayout *grid = new QGridLayout();
    
    QHBoxLayout *hbl = new QHBoxLayout();
    hbl->addStretch(2);
    hbl->addWidget(Confirm);
    hbl->addWidget(Cancel);
    hbl->addWidget(Apply);
    grid->addWidget(LabTitle,0,1);
    grid->addWidget(MenuList,1,0);
    grid->addWidget(SetPage,1,1);
    grid->addLayout(hbl,2,1);
//    grid->addLayout(hbl,1,1);
    setLayout(grid);
    
}

void DAConfigDialog::MenuListAddItem(const QString &text,QWidget *widget)
{
    
    QListWidgetItem * item = new QListWidgetItem();
    item->setTextAlignment(Qt::AlignVCenter);
    item->setSizeHint(QSize(0, 30));
    QPushButton *btn = new QPushButton(text);
    btn->setStyleSheet("border:0px;");
    MenuList->addItem(item);
    MenuList->setItemWidget(item,btn);
    if(widget != nullptr)
    {
        //    widget->PluginName();
        SetPage->addWidget(widget);
        MenuListBtn->addButton(btn,SetPage->indexOf(widget));
    }
}

void DAConfigDialog::MenuListAddItem(const QString &text, int index)
{
    QListWidgetItem * item = new QListWidgetItem();
    item->setTextAlignment(Qt::AlignVCenter);
    item->setSizeHint(QSize(0, 30));
    QPushButton *btn = new QPushButton(text);
    btn->setStyleSheet("border:0px;");
    MenuList->addItem(item);
    MenuList->setItemWidget(item,btn);
    MenuListBtn->addButton(btn,index);
}

void DAConfigDialog::BindBtnSlot()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5,15,0))
    connect(BtnGroup,&QButtonGroup::idClicked,this,&DAConfigDialog::BtnGroupClicked);
    connect(MenuListBtn,&QButtonGroup::idClicked,this,&DAConfigDialog::BtnMenuListClicked);
    MenuListBtn->idClicked(0);
#else
    connect(BtnGroup,qOverload<int>(&QButtonGroup::buttonClicked),this,&DAConfigDialog::BtnGroupClicked);
    connect(MenuListBtn,qOverload<int>(&QButtonGroup::buttonClicked),this,&DAConfigDialog::BtnMenuListClicked);
    MenuListBtn->button(0)->clicked();
#endif
}

void DAConfigDialog::resizeEvent(QResizeEvent *event)
{
    dasettings->SetValue(GENERAL_SIZE,event->size());
    dasettings->SaveConfig();
    qout << event->size();
}

void DAConfigDialog::MenuListTitle(const QString &title)
{
    LabTitle->setText(title);
}

void DAConfigDialog::BtnGroupClicked(int index)
{
    switch (index){
    case 0:
//        dasettings->SaveConfig();
{
#if 1
//qt 方式实现软件重启
    // 获取当前应用程序的路径和参数
    QString appPath = QCoreApplication::applicationFilePath();
    QStringList appArgs = QCoreApplication::arguments();
    // 启动一个新的实例来重新运行应用程序
    bool restart = QProcess::startDetached(appPath, appArgs);
    qout << "restart" << restart;
    this->close();
    if(restart)
    {
        exit(0);
    }
#else
    //c语言方式实现
    char* appPath = realpath("/proc/self/exe", NULL);
    char* const appArgs[] = { appPath, NULL };
    execv(appPath, appArgs);
    // execv 函数成功调用后，下面的代码不会执行
    qout << "Failed to restart the application.";
#endif
}
        break;
    case 1:
        this->close();
        break;
    case 2:
//        dasettings->SaveConfig();
        break;
    default:
        //一般不可能会出错、、、
        break;
    }
}

void DAConfigDialog::BtnMenuListClicked(int index)
{
    QString Title = MenuListBtn->button(index)->text();
    MenuListTitle(Title);
    SetPage->setCurrentIndex(index);
//    qout << __FUNCTION__ << " " <<index;
}
